﻿$(document).ready(function () {
    $.fn.dslabel = function (method, args) {
        var con = this;
        return $.controlInit("dslabel", con, method, args);
    }
    $.fn.dslabel.defaultOptions = {
        "close": false,
        "text": "",
        "attributes": null,
        "icon": "",
        "onClose": null
    };

    var methods = $.fn.dslabel.methods = {
        init: function (ctl, args) {
            ctl.each(function (i, el) {
                var jq = $(el);
                var options = $.extend({}, $.fn.dslabel.defaultOptions, args);
                jq.data("options", options);
                methods.draw(jq, options);
            });
        },
        options: function (jq, args) {
            return jq.data("options");
        },
        draw: function (jq, args) {
            var options = args;
            jq.attr("class", "dslabel");
            jq.css("cursor", "pointer");
            //jq.attr("href", "javascript:void(0)")
            var left = $("<SPAN class='dslabel-left'/>").appendTo(jq);
            var text = $("<SPAN class='dslabel-text'>" + args.text + "</SPAN>").appendTo(left);
            jq.data("textEl", text);
            if (options.icon) {
                text.css("padding-left", "20px");
                text.addClass(options.icon);
            }
            jq.bind("click", function (evt) {
                var evtTarget = $(evt.target);
                if (evtTarget.hasClass("label-delete")) {
                    if ($.isFunction(options.onClose)) {
                        options.onClose(jq);
                    }
                    cancel.remove();
                    jq.remove();
                }
                else {
                    if ($.isFunction(options.onClick)) {
                        options.onClick(jq);
                    }
                }
                return false;
            });
            jq.bind("contextmenu", function (evt) {
                if ($.isFunction(options.onContextMenu)) {
                    options.onContextMenu(jq, evt);
                    return false;
                }
            });
            if (options.close) {
                var cancel = $("<div class='label-delete' style='position:absolute;z-index:9999' />").appendTo(jq).hide();
                jq.bind("mouseover", function (evt) {
                    var isdrag = false;
                    if (DragManager) {
                        var dm = DragManager.currentDragManager();
                        if (dm && dm.isDragging) {
                            cancel.hide();
                            isdrag = true;
                        }
                        else {
                            isdrag = false;
                        }
                    }
                    if (!isdrag) {
                        var offset = jq.position();
                        var top = -8;
                        var left = jq.innerWidth() - 8;
                        cancel.css({ "top": top + "px", "left": left + "px" });
                        cancel.show();
                    }
                    evt.stopPropagation();
                });
                jq.bind("mouseout", function (evt) {
                    cancel.hide();
                    evt.stopPropagation();
                });
            }
        },
        setIcon: function (jq, args) {
            var options = methods.options(jq);
            var newicon = args;
            var textEl = jq.data("textEl");
            if (options.icon) {
                textEl.removeClass(options.icon);
            }
            if (newicon) {
                textEl.addClass(newicon);
            }
            else {
                textEl.css("padding-left", "0px");
            }
            options.icon = newicon;
        },
        setText: function (jq, args) {
            var options = methods.options(jq);
            var textEl = jq.data("textEl");
            textEl.html(args);
            options.text = args;
        },
        getAttributes: function (jq, args) {
            var options = methods.options(jq);
            return options.attributes;
        }
    };

});